package com.niiwoo.civet.trade.service.local.contract;

import com.niiwoo.civet.trade.dao.entity.FadadaContractTemplateFile;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.ProjectContract;
import com.niiwoo.civet.trade.dao.mapper.FadadaContractTemplateFileMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectContractMapperExt;
import com.niiwoo.civet.trade.enums.FadadaContractEnum;
import com.niiwoo.civet.trade.enums.FadadaContractTemplateEnum.TemplateTypeEnum;
import com.niiwoo.tripod.provider.exception.BizException;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FadadaContractContext {

	@Autowired
	private FadadaContractTemplateFileMapperExt contractTemplateFileMapperExt;
	
	@Autowired
	private SnowflakeIdWorker snowflakeIdWorker;
	@Autowired
	private ProjectContractMapperExt projectContractMapperExt;

	/**
	 * 合同处理 1.合同生成 2.合同签章 3.合同归档 4.保存合同数据
	 * 
	 * @param templateType
	 * @param bizContractNo
	 *            业务合同编号
	 * @param project
	 * @param userId
	 * @return
	 */
	public ProjectContract contractHandler(TemplateTypeEnum templateType, String bizContractNo, Project project,
			String userId) throws Exception{
		FadadaContractTemplateFile contractTemplateFile = contractTemplateFileMapperExt
				.queryFadadaContractTemplateFileByTemplateType(templateType.getType());
		if (contractTemplateFile == null) {
			// 借款合同模板不存在
			throw new BizException("TRD_700007", templateType + " templateType not exists.");
		}

		return FadadaContractFactory.buildFadadaContract(templateType).contractHandler(contractTemplateFile,
				bizContractNo, project, userId);

	}
	
	public ProjectContract initContractRecord(Long projectId, String userId, String contractNo, Byte contractType,
			Byte tradeType, Long orderId) {
		ProjectContract projectContract = new ProjectContract();
		projectContract.setId(snowflakeIdWorker.nextId());
		projectContract.setProjectId(projectId);
		projectContract.setUserId(userId);
		projectContract.setContractNo(contractNo);
		projectContract.setContractType(contractType);
		projectContract.setTradeType(tradeType);
		projectContract.setOrderId(orderId);
		projectContract.setStatus(FadadaContractEnum.StatusEnum.NO.getStatus());
		projectContract.setIsAuto(FadadaContractEnum.IsAutoEnum.AUTO.getType());
		projectContract.setCreateTime(new Date());
		projectContract.setUpdateTime(new Date());

		return projectContract;
	}
	@Transactional
	public void saveProjectContract(ProjectContract projectContract) {
		projectContractMapperExt.insertSelective(projectContract);
	}
	
	@Transactional
	public void updateProjectContract(ProjectContract projectContract) {
		projectContractMapperExt.updateByPrimaryKeySelective(projectContract);
	}
}
